package com.czy.owner.ui.information;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.FindActivityRecommondAdapter;
import com.czy.owner.api.BannerToFindActivityApi;
import com.czy.owner.api.FindActivityListApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.BannerToActivityModel;
import com.czy.owner.entity.FindActivityModel;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.LuckDrawWebviewActivity;
import com.czy.owner.ui.NewWebViewActivity;
import com.czy.owner.ui.WebViewActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_GET;
import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;

/**
 * Created by Raven on 2017/12/15.
 */

public class RecommendActivityListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;

    private FindActivityRecommondAdapter findActivityRecommondAdapter;
    private Handler handler = new Handler();
    private int page = 1;

    @Override
    public void onRefresh() {

        easyRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                findActivityRecommondAdapter.clear();
                page = 1;
                getActivityRecommond();
//                bannerToActivity();
            }
        }, 1000);

    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //加载更多
                if (!PhoneUtils.isNetworkConnected(RecommendActivityListActivity.this)) {
                    findActivityRecommondAdapter.pauseMore();
                    easyRecyclerView.showError();
                    return;
                }
                page++;
                getActivityRecommond();

            }
        }, 1000);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.more_activity);
//        RxBus.getDefault().register(this);
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        easyRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        easyRecyclerView.setAdapterWithProgress(findActivityRecommondAdapter = new FindActivityRecommondAdapter(this));
        findActivityRecommondAdapter.setMore(R.layout.view_more, this);
        findActivityRecommondAdapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                //adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                findActivityRecommondAdapter.resumeMore();
            }
        });

        findActivityRecommondAdapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                findActivityRecommondAdapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                findActivityRecommondAdapter.resumeMore();

            }
        });

        findActivityRecommondAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<FindActivityModel>() {
            @Override
            public void onItemClick(View view, int position, FindActivityModel model) {

                if (model.getLinkType().equals("url")) {
                    MyLog.d("RAVEN","url=========="+model.getUrl());
                    if (model.getUrl().contains("own/shop/luckDraw")) {
                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(Constants.APP_API_BASE + Constants.OWN_SHOP_LUCKDRAW);
                        webViewRequestParam.setRequestMethod(REQUEST_POST);
                        webViewRequestParam.setParams("session=" + UserHelper.getInstance().getSession(RecommendActivityListActivity.this));
                        webViewRequestParam.setTitle("转盘抽奖");
                        Intent intent = new Intent(RecommendActivityListActivity.this, LuckDrawWebviewActivity.class);
                        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                        startActivity(intent);
                    } else if(model.getUrl().contains("/own/shop/doubleEleven")){
                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(model.getUrl());
                        webViewRequestParam.setRequestMethod(REQUEST_GET);
                        Intent intent = new Intent(RecommendActivityListActivity.this, WebViewActivity.class);
                        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                        intent.putExtra("type","doubleEleven");
                        intent.putExtra("mFromTypeId",model.getGoodsActivityId()+"");
                       startActivity(intent);
                    }else if(model.getUrl().contains("/own/shop/sunburn")){
                        WebViewRequestParam webViewRequestParam3 = new WebViewRequestParam(Constants.APP_API_BASE + Constants.OWN_SHOP_SUNBURN);
                        webViewRequestParam3.setRequestMethod(REQUEST_GET);
                        webViewRequestParam3.setTitle("晒评价,领现金券");
                        Intent intent3 = new Intent(RecommendActivityListActivity.this, NewWebViewActivity.class);
                        intent3.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam3);
                        startActivity(intent3);
                    }else if(model.getUrl().contains("own/shop/club")){
                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(Constants.APP_API_BASE + Constants.OWN_SHOP_CLUB);
                        webViewRequestParam.setRequestMethod(REQUEST_GET);
                        webViewRequestParam.setTitle("FM908");
                        Intent intent1 = new Intent(RecommendActivityListActivity.this, NewWebViewActivity.class);
                        intent1.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                        startActivity(intent1);
                    }else if(model.getUrl().contains("own/shop/businessDaily")){
                        WebViewRequestParam webViewRequestParam4 = new WebViewRequestParam(Constants.APP_API_BASE + Constants.OWN_SHOP_BUSINESSDAILY);
                        webViewRequestParam4.setRequestMethod(REQUEST_GET);
                        webViewRequestParam4.setTitle("安徽商报");
                        Intent intent4 = new Intent(RecommendActivityListActivity.this, NewWebViewActivity.class);
                        intent4.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam4);
                        startActivity(intent4);
                    }
                    else{
                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(model.getUrl());
                        webViewRequestParam.setRequestMethod(REQUEST_GET);
                        Intent intent = new Intent(RecommendActivityListActivity.this, WebViewActivity.class);
                        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                        intent.putExtra("type","袋鼠管家");
                        intent.putExtra("mFromTypeId",model.getGoodsActivityId()+"");
                        startActivity(intent);
                        MyLog.e("yang","比价活动");
                    }


                } else {
                    Intent ti = new Intent(RecommendActivityListActivity.this, InformationDetailActivity.class);
                    ti.putExtra("title", model.getTitle());
                    ti.putExtra("content", model.getDescription1());
                    ti.putExtra("imgUrl", model.getLogo());
                    ti.putExtra("infoId", model.getGoodsActivityId());
                    ti.putExtra("collectType", "activity");
                    startActivity(ti);
                }

            }
        });
        easyRecyclerView.setRefreshListener(this);
        onRefresh();
    }

    //    推荐banner到活动
    private void bannerToActivity() {

        BannerToFindActivityApi bannerToFindActivityApi = new BannerToFindActivityApi(new HttpOnNextListener<List<BannerToActivityModel>>() {
            @Override
            public void onNext(List<BannerToActivityModel> list) {
                MyLog.e("yang", "list==" + list.size());
                if (list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        FindActivityModel model = new FindActivityModel();
                        model.setTitle(list.get(i).getDescription());
                        model.setDescription1(list.get(i).getDescription());
                        model.setLogo(list.get(i).getPicUrl());
                        model.setLinkType(list.get(i).getContactType());
                        model.setUrl(list.get(i).getContactId());
                        findActivityRecommondAdapter.add(model);

                    }
                    findActivityRecommondAdapter.notifyDataSetChanged();
                }

            }
        }, this);
        bannerToFindActivityApi.setSession(UserHelper.getInstance().getSession(this));
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(bannerToFindActivityApi);
    }

    @Override
    protected void updateViews() {

    }

        private void getActivityRecommond() {
        FindActivityListApi findActivityListApi = new FindActivityListApi(new HttpOnNextListener<List<FindActivityModel>>() {
            @Override
            public void onNext(List<FindActivityModel> list) {
                setActivityRecommonData(list);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<FindActivityModel>>>() {
                }.getType();
                BaseResultEntity<List<FindActivityModel>> cacheList = gson.fromJson(cache, type);
                setActivityRecommonData(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(RecommendActivityListActivity.this, e.getMessage());
            }
        }, RecommendActivityListActivity.this, page + "");
        findActivityListApi.setSession(UserHelper.getInstance().getSession(RecommendActivityListActivity.this));
        findActivityListApi.setPage(page + "");
        findActivityListApi.setSize("10");
        findActivityListApi.setTitle("");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(findActivityListApi);
    }

        private void setActivityRecommonData(List<FindActivityModel> list) {
        if (list.size() < 9) {
            findActivityRecommondAdapter.stopMore();
        }

        if (list.size() == 0 && !PhoneUtils.isNetworkConnected(this)) {
            findActivityRecommondAdapter.pauseMore();
            easyRecyclerView.showError();
            return;
        }
        findActivityRecommondAdapter.addAll(list);

    }

}
